/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import java.util.Vector;
import Models.ThamSoHT_Model;
import Models.NhatKyHT_Model;
import Models.NhanVien_Model;
import Beans.NhanVien;
import Beans.ThamSoHT;
/**
 *
 * @author Venus
 */
public class NhanVien_Controller extends HttpServlet {
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            if (session.getAttribute("langid") == null) {
                response.sendRedirect("index.jsp");
                return;
            }
            session.removeAttribute("DSMA");
            session.removeAttribute("DSNMA");
            session.removeAttribute("item");
            String action = request.getParameter("action");
            int langid = (Integer) session.getAttribute("langid");
            if (action.equals("Show")) {
                String item = request.getParameter("item");
                NhanVien_Model nvm = new NhanVien_Model();
                Vector v1 = new Vector();
                Vector v2 = new Vector();
                v1 = nvm.LayDSNV();
                v2 = nvm.LayDSNNV(langid);
                request.setAttribute("DSNV", v1);
                request.setAttribute("NhomNV", v2);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/newuser.jsp");
                rd.forward(request, response);
            } else if (action.equals("ShowDSNV")) {
                String item = request.getParameter("item");
                NhanVien_Model nvm = new NhanVien_Model();
                Vector v1 = new Vector();
                v1 = nvm.LayDSNV();
                request.setAttribute("DSNV", v1);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listuser.jsp");
                rd.forward(request, response);
            } else if (action.equals("ShowTaoNV")) {
                NhanVien_Model nvm = new NhanVien_Model();
                Vector v = new Vector();
                v = nvm.LayDSNNV(langid);
                request.setAttribute("NhomNV", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/showcreateuser.jsp");
                rd.forward(request, response);
            } else if (action.equals("CreateUser")) {
                String tennv = request.getParameter("tennv");
                String username = request.getParameter("username");
                String nnv = request.getParameter("nnv");
                String diachi = request.getParameter("diachi");
                String sodt = request.getParameter("sodt");
                String ngayvaolam = request.getParameter("ngayvaolam");
                NhanVien_Model.TaoNhanVien(tennv, username, nnv, diachi, sodt, ngayvaolam);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(2, nv.getMaNV(), "user");
                out.print("success");
            } else if (action.equals("UpdateNNV")) {
                String mannv = request.getParameter("mannv");
                String manv = request.getParameter("manv");
                String item = request.getParameter("item");
                NhanVien_Model.UpdateNNV(mannv, manv);                
                NhanVien_Model nvm = new NhanVien_Model();
                Vector v = new Vector();
                v = nvm.LayDSNV();
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "groupuser");
                request.setAttribute("item", item);
                request.setAttribute("DSNV", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listuser.jsp");
                rd.forward(request, response);
            } else if (action.equals("UpdateStatusNV")) {
                String status = request.getParameter("status");
                String manv = request.getParameter("manv");
                String item = request.getParameter("item");
                NhanVien_Model.UpdateStatusNV(status, manv);                
                NhanVien_Model nvm = new NhanVien_Model();
                Vector v = new Vector();
                v = nvm.LayDSNV();
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "statususer");
                request.setAttribute("item", item);
                request.setAttribute("DSNV", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listuser.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortHoTenNV")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhanVien_Model nvm = new NhanVien_Model();
                Vector v = new Vector();
                v = nvm.SortHoTenNV( order);
                request.setAttribute("DSNV", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortuser.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortNgayVaoLam")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhanVien_Model nvm = new NhanVien_Model();
                Vector v = new Vector();
                v = nvm.SortNgayVaoLam( order);
                request.setAttribute("DSNV", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortuser.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortNhomNV")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhanVien_Model nvm = new NhanVien_Model();
                Vector v = new Vector();
                v = nvm.SortNhomNV(langid, order);
                request.setAttribute("DSNV", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortuser.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortTTNV")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhanVien_Model nvm = new NhanVien_Model();
                Vector v = new Vector();
                v = nvm.SortTTNV(order);
                request.setAttribute("DSNV", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "4");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortuser.jsp");
                rd.forward(request, response);
            } else if (action.equals("SearchUser")) {
                String HOTEN = request.getParameter("HOTEN");
                String NGAYVAOLAM = request.getParameter("NGAYVAOLAM");
                String NHOMNHANVIEN = request.getParameter("NHOMNHANVIEN");
                String TINHTRANG = request.getParameter("TINHTRANG");
                String item = request.getParameter("item");
                NhanVien_Model nvm = new NhanVien_Model();
                Vector v = new Vector();
                v = nvm.SearchUser(HOTEN, NGAYVAOLAM, NHOMNHANVIEN, TINHTRANG, langid);
                request.setAttribute("HOTEN", HOTEN);
                request.setAttribute("NGAYVAOLAM", NGAYVAOLAM);
                request.setAttribute("NHOMNHANVIEN", NHOMNHANVIEN);
                request.setAttribute("TINHTRANG", TINHTRANG);
                request.setAttribute("item", item);
                request.setAttribute("DSNV", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssearchuser.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchHoTenNV")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String HOTEN = request.getParameter("HOTEN");
                String NGAYVAOLAM = request.getParameter("NGAYVAOLAM");
                String NHOMNHANVIEN = request.getParameter("NHOMNHANVIEN");
                String TINHTRANG = request.getParameter("TINHTRANG");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhanVien_Model nvm = new NhanVien_Model();
                Vector v = new Vector();
                v = nvm.SortSearchHoTenNV(langid, order, HOTEN, NGAYVAOLAM, NHOMNHANVIEN, TINHTRANG);
                request.setAttribute("DSNV", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("HOTEN", HOTEN);
                request.setAttribute("NGAYVAOLAM", NGAYVAOLAM);
                request.setAttribute("NHOMNHANVIEN", NHOMNHANVIEN);
                request.setAttribute("TINHTRANG", TINHTRANG);
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchuser.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchNgayVaoLam")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String HOTEN = request.getParameter("HOTEN");
                String NGAYVAOLAM = request.getParameter("NGAYVAOLAM");
                String NHOMNHANVIEN = request.getParameter("NHOMNHANVIEN");
                String TINHTRANG = request.getParameter("TINHTRANG");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhanVien_Model nvm = new NhanVien_Model();
                Vector v = new Vector();
                v = nvm.SortSearchNgayVaoLam(langid, order, HOTEN, NGAYVAOLAM, NHOMNHANVIEN, TINHTRANG);
                request.setAttribute("DSNV", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("HOTEN", HOTEN);
                request.setAttribute("NGAYVAOLAM", NGAYVAOLAM);
                request.setAttribute("NHOMNHANVIEN", NHOMNHANVIEN);
                request.setAttribute("TINHTRANG", TINHTRANG);
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchuser.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchNhomNV")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String HOTEN = request.getParameter("HOTEN");
                String NGAYVAOLAM = request.getParameter("NGAYVAOLAM");
                String NHOMNHANVIEN = request.getParameter("NHOMNHANVIEN");
                String TINHTRANG = request.getParameter("TINHTRANG");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhanVien_Model nvm = new NhanVien_Model();
                Vector v = new Vector();
                v = nvm.SortSearchNhomNV(langid, order, HOTEN, NGAYVAOLAM, NHOMNHANVIEN, TINHTRANG);
                request.setAttribute("DSNV", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("HOTEN", HOTEN);
                request.setAttribute("NGAYVAOLAM", NGAYVAOLAM);
                request.setAttribute("NHOMNHANVIEN", NHOMNHANVIEN);
                request.setAttribute("TINHTRANG", TINHTRANG);
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchuser.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchTTNV")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String HOTEN = request.getParameter("HOTEN");
                String NGAYVAOLAM = request.getParameter("NGAYVAOLAM");
                String NHOMNHANVIEN = request.getParameter("NHOMNHANVIEN");
                String TINHTRANG = request.getParameter("TINHTRANG");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhanVien_Model nvm = new NhanVien_Model();
                Vector v = new Vector();
                v = nvm.SortSearchTTNV(langid, order, HOTEN, NGAYVAOLAM, NHOMNHANVIEN, TINHTRANG);
                request.setAttribute("DSNV", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "4");
                request.setAttribute("HOTEN", HOTEN);
                request.setAttribute("NGAYVAOLAM", NGAYVAOLAM);
                request.setAttribute("NHOMNHANVIEN", NHOMNHANVIEN);
                request.setAttribute("TINHTRANG", TINHTRANG);
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchuser.jsp");
                rd.forward(request, response);
            } else if (action.equals("Detail")) {
                String manv = request.getParameter("manv");
                NhanVien_Model nvm = new NhanVien_Model();
                Vector v = new Vector();
                v = nvm.Detail(manv);
                request.setAttribute("detail", v);
                request.setAttribute("manv", manv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/detailuser.jsp");
                rd.forward(request, response);
            } else if (action.equals("PhanQuyenCaNhan")) {
                String manv = request.getParameter("manv");
                int MaNNV = NhanVien_Model.LayMaNNV(Integer.parseInt(manv));
                request.setAttribute("manv", manv);
                request.setAttribute("mannv", Integer.toString(MaNNV));
                RequestDispatcher rd = request.getRequestDispatcher("admin/phanquyencanhan.jsp");
                rd.forward(request, response);
            } else if (action.equals("PhanQuyenNhom")) {
                NhanVien_Model nvm = new NhanVien_Model();
                Vector v = new Vector();
                v = nvm.LayDSNNV(langid);
                request.setAttribute("nhomnhanvien", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/phanquyennhom.jsp");
                rd.forward(request, response);
            }  else if (action.equals("UpdateQuyenCaNhan")) {
                String manv = request.getParameter("manv");
                String yes = request.getParameter("yes");
                String no = request.getParameter("no");
                if(!yes.equals("")){
                    String yess[] = yes.split("-");
                    int j = 0;
                    while (j < yess.length) {
                        NhanVien_Model.AddQuyenCaNhan(yess[j], manv);
                        j = j + 1;
                    }
                }
                if(!no.equals("")){
                    String noo[] = no.split("-");
                    int i = 0;
                    while (i < noo.length) {
                        NhanVien_Model.RemoveQuyenCaNhan(noo[i], manv);
                        i = i + 1;
                    }
                }
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "persondecentra");
                out.print("success");
            } else if (action.equals("UpdateQuyenNhom")) {
                String yes = request.getParameter("yes");
                String no = request.getParameter("no");
                String mayes = request.getParameter("mayes");
                String mano = request.getParameter("mano");
                if(!yes.equals("") && !mayes.equals("")){
                    String yess[] = yes.split("-");
                    String mayess[] = mayes.split("-");
                    int j = 0;
                    while (j < yess.length) {
                        NhanVien_Model.AddQuyenNhom(yess[j], mayess[j]);
                        j = j + 1;
                    }
                }
                if(!no.equals("") && !mano.equals("")){
                    String noo[] = no.split("-");
                    String manoo[] = mano.split("-");
                    int i = 0;
                    while (i < noo.length) {
                        NhanVien_Model.RemoveQuyenNhom(noo[i], manoo[i]);
                        i = i + 1;
                    }
                }
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "groupdecentra");
                out.print("success");
            } else if (action.equals("UpdateDetailNV")) {
                String tennv = request.getParameter("tennv");
                String diachi = request.getParameter("diachi");
                String sodt = request.getParameter("sodt");
                String ngayvaolam = request.getParameter("ngayvaolam");
                String manv = request.getParameter("manv");
                NhanVien_Model.UpdateDetailNV(manv, tennv, diachi, sodt, ngayvaolam);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "userinfo");
                out.print("success");
            } else if (action.equals("CheckQuyenCaNhan")) {
                String maquyen = request.getParameter("maquyen");
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                if (NhanVien_Model.CheckQuyen(Integer.parseInt(maquyen), nv.getMaNV())) {
                    out.print("yes");
                } else {
                    out.print("no");
                }
            }
            else if (action.equals("Changepass")) {
                String tennv = request.getParameter("tennv");
                String newpass = request.getParameter("newpass");
                String diachi = request.getParameter("diachi");
                String sodt = request.getParameter("sodt");
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                if(NhanVien_Model.Changepass(tennv, newpass, diachi, sodt, nv.getMaNV())){                    
                    session.removeAttribute("nv");
                    session.removeAttribute("langid");
                    session.removeAttribute("item");
                    session.removeAttribute("DSNMA");
                    session.removeAttribute("DSMA");
                    response.sendRedirect("login.jsp");
                }
                
                else{
                    out.print("false");
                }
            }
            else if (action.equals("CapNhatTKPV")) {
                String tennv = request.getParameter("tennv");
                String newpass = request.getParameter("newpass");
                String diachi = request.getParameter("diachi");
                String sodt = request.getParameter("sodt");
                String manv = request.getParameter("manv");
                String dsts = "'updatesuc','updatefal'";
                ThamSoHT[] mdsts = ThamSoHT_Model.DSThamSo(dsts, langid);
                String updatefal = mdsts[0].getTenTS();
                String updatesuc = mdsts[1].getTenTS();
                int nv = Integer.parseInt(manv);
                if(NhanVien_Model.Changepass(tennv, newpass, diachi, sodt, nv)){
                     out.print(updatesuc);
                }

                else{
                    out.print(updatefal);
                }
            }
            
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
